package com.wontube.smr.web.controller.admin;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.wontube.client.extension.remote.cache.ICacheHandler;
import com.wontube.client.remote.Remoting;
import com.wontube.smr.client.IMovieHandler;
import com.wontube.smr.client.model.po.Movie;
import com.wontube.smr.client.model.po.Tool;
import com.wontube.smr.client.model.po.ToolDetail;
import com.wontube.smr.web.client.admin.IAdminHandler;

/**
 * 管理员控制器
 * 
 * @author hewc@wondershare.cn
 * 
 */
@Controller
@RequestMapping(value = "/admin", method = RequestMethod.GET)
public final class AdminController {
	
	@RequestMapping("")
	public ModelAndView adminHome(){
		return new ModelAndView("admin/admin_index");
	}
	
	@RequestMapping(value = "/banner", method = RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView mv = new ModelAndView();
		//Remoting<IMovieHandler> remoting = new Remoting<IMovieHandler>();
		//IMovieHandler handler = remoting.createService(IMovieHandler.class);
		//PagenateInfo pageInfo = new PagenateInfo(1, 10);
		//MovieItems videoItems = handler.selectHuluWeekPopular(pageInfo);

		//mv.addObject("huluItems", videoItems);
		mv.setViewName("admin/admin_video");
		return mv;
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public ModelAndView videoShow(@RequestParam(value = "id", required = false) String videoId,
			@RequestParam(value = "title", required = false) String title,
			@RequestParam(value = "url", required = false) String bannerUrl) {
		ModelAndView mv = new ModelAndView();
		Remoting<IAdminHandler> remoting = new Remoting<IAdminHandler>();
		IAdminHandler handler = remoting.createService(IAdminHandler.class);

		Movie movie = new Movie();
		movie.setId(videoId);
		movie.setBannerImgUrl(bannerUrl);
		handler.updateVideoBanner(movie);
		mv = index();

		return mv;
	}

	@RequestMapping(value = "/deleteMoveCache", method = RequestMethod.POST)
	public ModelAndView deleteMoveCache() {
		ModelAndView mv = new ModelAndView();
		Remoting<ICacheHandler> remoting = new Remoting<ICacheHandler>();
		remoting.setDataFormat(Remoting.dataFormatHessian);
		ICacheHandler handler = remoting.createService(ICacheHandler.class);
		String clazzName = IMovieHandler.class.getName();
		String methodName = "listWebHomeItems";

		// PagenateInfo pageInfo = new PagenateInfo(1, 10);
		Object[] args = new Object[] {};
		handler.delete(clazzName, methodName, args);

		mv.setViewName("admin/admin_scuess");

		return mv;
	}

	@RequestMapping(value = "/toolsetting", method = RequestMethod.GET)
	public ModelAndView toolSettingIndex() {
		ModelAndView mv = new ModelAndView();
		Remoting<IAdminHandler> remoting = new Remoting<IAdminHandler>();
		IAdminHandler handler = remoting.createService(IAdminHandler.class);
		mv.addObject("websiteItems", handler.selectTool("Website"));
		mv.addObject("pcItems", handler.selectTool("Pc"));
		mv.addObject("mobileItems", handler.selectTool("Mobile"));

		mv.setViewName("admin/admin_tool");

		return mv;
	}

	@RequestMapping(value = "toolsetting/add", method = RequestMethod.POST)
	public ModelAndView toolSettingAdd(@RequestParam(value = "link", required = false) String link,
			@RequestParam(value = "thumbnail", required = false) String thumbnail,
			@RequestParam(value = "sort_index", required = false) Integer sortIndex,
			@RequestParam(value = "descripiton", required = false) String descripiton,
			@RequestParam(value = "tool_type", required = false) String toolType,
			@RequestParam(value = "id", required = false) String id,
			@RequestParam(value = "title", required = false) String title) {
		ModelAndView mv = new ModelAndView();
		if (toolType != null && !toolType.trim().equals("")) {
			Remoting<IAdminHandler> remoting = new Remoting<IAdminHandler>();
			IAdminHandler handler = remoting.createService(IAdminHandler.class);
			Tool tool = new Tool();
			tool.setId(id);
			tool.setLink(link);
			tool.setThumbnail(thumbnail);
			tool.setSortIndex(sortIndex);
			tool.setDescripiton(descripiton);
			tool.setToolType(toolType);
			tool.setTitle(title);
			handler.insertTool(tool);
		}

		mv = toolSettingIndex();
		return mv;
	}

	@RequestMapping(value = "toolsetting/saveOrUpdateToolDetail", method = RequestMethod.POST)
	public ModelAndView saveOrUpdateToolDetail(@RequestParam(value = "thumbnail", required = false) String thumbnail,
			@RequestParam(value = "bigPic", required = false) String bigPic,
			@RequestParam(value = "toolDetailId", required = false) String toolDetailId,
			@RequestParam(value = "toolId", required = false) String toolId) {
		ModelAndView mv = new ModelAndView();
		if (thumbnail != null && !thumbnail.trim().equals("")) {
			Remoting<IAdminHandler> remoting = new Remoting<IAdminHandler>();
			IAdminHandler handler = remoting.createService(IAdminHandler.class);
			ToolDetail tool = new ToolDetail();
			tool.setId(toolDetailId);
			tool.setToolId(toolId);
			tool.setThumbnail(thumbnail);
			tool.setBigPic(bigPic);
			handler.saveOrUpdateToolDetail(tool);
		}

		mv = toolSettingIndex();
		return mv;
	}

	@RequestMapping(value = "toolsetting/{id}", method = RequestMethod.POST)
	public ModelAndView selectToolById(@PathVariable("id") String id) {
		ModelAndView mv = new ModelAndView();
		Remoting<IAdminHandler> remoting = new Remoting<IAdminHandler>();
		IAdminHandler handler = remoting.createService(IAdminHandler.class);
		Tool tool = handler.selectToolById(id);
		mv.addObject("tool", tool);
		mv.setViewName("admin/admin_update");
		return mv;
	}

	@RequestMapping(value = "toolsetting/delete/{id}", method = RequestMethod.GET)
	public ModelAndView deleteToolById(@PathVariable("id") String id) {
		ModelAndView mv = new ModelAndView();
		Remoting<IAdminHandler> remoting = new Remoting<IAdminHandler>();
		IAdminHandler handler = remoting.createService(IAdminHandler.class);
		handler.deleteTool(id);
		mv = toolSettingIndex();
		return mv;
	}

	@RequestMapping(value = "toolsetting/editpic/{id}", method = RequestMethod.GET)
	public ModelAndView editpic(@PathVariable("id") String id) {
		ModelAndView mv = new ModelAndView();
		Remoting<IAdminHandler> remoting = new Remoting<IAdminHandler>();
		IAdminHandler handler = remoting.createService(IAdminHandler.class);
		List<ToolDetail> toolDetails = handler.selectToolDetailById(id);
		mv.addObject("toolId", id);
		mv.addObject("toolDetails", toolDetails);
		mv.setViewName("admin/tool/detail/admin_detail");
		return mv;
	}
}
